package com.ruoyi.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.cg.pojo.Group;
import com.ruoyi.common.core.cg.pojo.Store;
import com.ruoyi.common.core.cg.result.Queryparam;
import com.ruoyi.common.core.cg.result.Result;
import com.ruoyi.manage.mapper.GroupMapper;
import com.ruoyi.manage.mapper.OrderMapper;
import com.ruoyi.manage.mapper.StoreMapper;
import com.ruoyi.manage.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 马景昌
 * @create 2025/03/10
 * @description
 */
@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public Result pageshow(Integer pageNum, Integer pageSize, Group group) {
        if(pageNum==null && pageSize==null){
            pageNum=1;
            pageSize=5;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Group> pageshow = groupMapper.pageshow(group);
        for (Group group1 : pageshow) {
            Store store = storeMapper.selectBygid(group1.getId());
            if(store==null){
                group1.setStoreName("中间表没有添加");
            }else {
                group1.setStoreName(store.getName());
            }
        }
        PageInfo<Group> pageInfo = new PageInfo<>(pageshow);
        return new Result(true,"查询成功",pageInfo);
    }

    @Override
    public Result put0(Integer id) {
        boolean b = groupMapper.put0(id);
        if(!b){
            return new Result(false,"修改失败");
        }
        return new Result(true,"修改成功");
    }

    @Override
    public Result put1(Integer id) {
        boolean b = groupMapper.put1(id);
        if(!b){
            return new Result(false,"修改失败");
        }
        return new Result(true,"修改成功");
    }

    @Override
    public PageInfo<Group> GroupApp(Queryparam queryparam) {
        PageHelper.startPage(queryparam.getPageNum(),queryparam.getPageSize());
        List<Group> list = groupMapper.SelecrGroup(queryparam);
        for (Group group : list) {
            Store store = storeMapper.selectBygid(group.getId());
            if(store==null){
                group.setStoreName("中间表没有添加");
            }else {
                group.setStoreName(store.getName());
            }

            Integer integer = orderMapper.selectordercount0(group.getId());
            group.setItemStatus0(integer);
            Integer integer1 = orderMapper.selectordercount1(group.getId());
            group.setItemStatus1(integer1);
        }
        PageInfo<Group> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Result info(Integer id) {
        Group info = groupMapper.info(id);
        return new Result(true,"查询成功",info);
    }

    @Override
    public Result leadergroup(Integer pageNum, Integer pageSize, Group group) {
        PageHelper.startPage(pageNum,pageSize);
        List<Group> leadergroup = groupMapper.leadergroup(group);
        for (Group group1 : leadergroup) {
            Store store = storeMapper.selectBygid(group1.getId());
            if(store==null){
                group1.setStoreName("中间表没有添加");
            }else {
                group1.setStoreName(store.getName());
            }
        }
        PageInfo<Group> pageInfo = new PageInfo<>(leadergroup);
        return new Result(true,"查询成功",pageInfo);
    }

}
